/* eslint-disable camelcase */
/*
 * @Author: lp
 * @Date: 2022-01-11 18:05:07
 * @LastEditTime: 2022-06-14 10:37:19
 * @LastEditors: lp 654528967@qq.com
 * @Description: 用户相关api
 * @FilePath: \nien-vue3-admin\src\api\user\index.ts
 */
import request from '@/utils/request'
import { desEncrypt } from '@/utils/crypto'

export interface loginParams {
  [key: string]: string;
}
export interface UserInterface {
  access_token: string;
  expires_in: number;
  token_type: string;
  refresh_token: string;
}

const clientId = 'logisitics_web' 
// const clientId = 'logisitics_client' 
const clientSecret = '123456'
const scope = 'server'
export const getCodeImg = () => request({
  url: '/code',
})
export const login = (params: loginParams) => { 
  const req = JSON.parse(JSON.stringify(params)) 
  req.password = desEncrypt(req.password)
  req.username = desEncrypt(req.username)
  return request<UserInterface>({
    url: '/auth/oauth/token',
    method: 'post',
    params: {
      ...req,
      client_id: clientId,
      client_secret: clientSecret,
      scope,
      grant_type: 'password',
    },
  })
}
export const refreshToken = (refreshToken: string) => request<UserInterface>({
  url: '/auth/oauth/token',
  method: 'post',
  params: { client_id: clientId, client_secret: clientSecret, grant_type: 'refresh_token', scope, refresh_token: refreshToken },
})

export const getInfo = () => request({
  url: '/system/user/getInfo',
})
export const getRouters = () => request({
  url: '/system/menu/routers',
})
export const logout = () => request({
  url: '/auth/token/logout',
  method: 'delete',
})

export const getPhone = (params) => request({ url: '/system/user/phone', params })
export const getSms = (params) => request({ url: '/system/user/sms', params })
export const updatePassword = (data) => request({ url: '/system/user/updatePassword', data, method: 'put' })
export const queryFactory = (userName) => request({ url: `/system/user/company/${ userName }` })
// 切换公司
export const companyUpdate = (data) => request({ url: 'system/user/company/update', method: 'put', data })
